package cn.zhpdmmz.service.impl;

import cn.zhpdmmz.entity.*;
import cn.zhpdmmz.mapper.*;
import cn.zhpdmmz.service.IReservationService;
import cn.zhpdmmz.utils.CreatCheckInNo;
import cn.zhpdmmz.vo.ReservationVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Reservation)表服务实现类
 *
 * @author CJW
 * @since 2022-04-23 01:27:42
 */
@Service("reservationService")
public class ReservationServiceImpl implements IReservationService {
    private String checkInNo;
    @Resource
    private ReservationMapper reservationMapper;

    Map<String,List<Customer>> mapCache = new HashMap<>(); //缓存数据

    @Resource
    private CheckInformationMapper checkInformationMapper;

    @Resource
    private CheckInformationDetailsMapper checkInformationDetailsMapper;

    private CheckInformation checkInformation;
    @Resource
    private RoomStatusMapper roomStatusMapper;
    @Resource
    private HotelRoomMapper hotelRoomMapper;

    private HotelRoom hotelRoom;

    String customerPhone,roomNo;
    Integer roomTypeId;

    private Reservation reservation;
    /**
     * 通过ID查询单条数据
     *
     * @param reservationId 主键
     * @return 实例对象
     */
    @Override
    public Reservation queryById(Integer reservationId) {
        return this.reservationMapper.queryById(reservationId);
    }

    /**
     * 查询指定行
     *
     * @param reservation 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Reservation> queryAll(Reservation reservation) {
        return this.reservationMapper.queryAll(reservation);
    }

    /**
     * 新增数据
     *
     * @param reservation 实例对象
     * @return 实例对象
     */
    @Override
    public Reservation insert(Reservation reservation) {
        this.reservationMapper.insert(reservation);
        return reservation;
    }

    /**
     * 修改数据
     *
     * @param reservation 实例对象
     * @return 实例对象
     */
    @Override
    public Reservation update(Reservation reservation) {
        this.reservationMapper.update(reservation);
        return this.queryById(reservation.getReservationId());
    }

    /**
     * 通过主键删除数据
     *
     * @param reservationId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer reservationId) {
        return this.reservationMapper.deleteById(reservationId) > 0;
    }


//  开启缓存的方法，顾客的集合
    private Map<String, List<Customer>> createMapCache(String checkInNo){

        List<Customer> customerList = new ArrayList<>();
        mapCache.put(checkInNo,customerList);
        return mapCache;
    }
    /**
     * 步骤一
     * 添加客户
     *
     * @param reservationVo 主键
     * @return 是否成功
     */
    @Override
    public Map<String, Object> addCustomer(ReservationVo reservationVo) {
        //通过判断checkInNo是否存在开启新的缓存
        if(checkInNo == null){
//            新增checkInNo
            CreatCheckInNo creatCheckInNo = new CreatCheckInNo();
            checkInNo = creatCheckInNo.newCheckInNo();
//            新增入住订单对象
            checkInformation = new CheckInformation();
            checkInformation.setCheckInformationNo(checkInNo);
//            插入数据库
            this.checkInformationMapper.insert(checkInformation);
//            开启新的缓存
            this.createMapCache(checkInNo);
        }
        //待入住顾客列表
        List<Customer> customerList = mapCache.get(checkInNo);
        //新的顾客
        Customer customer = new Customer();
        //根据最后一位数判断男女
        String identifyCard;
        identifyCard = reservationVo.getIdentifyCard();
        if(Integer.parseInt(identifyCard) % 2 == 0){
            customer.setCustomerSex("女");
        }else
            customer.setCustomerSex("男");
//        设置顾客属性
        customer.setCustomerName(reservationVo.getCustomerName());
        customer.setIdentifyCard(reservationVo.getIdentifyCard());

        //将新顾客添加到列表里
        customerList.add(customer);
        mapCache.put(checkInNo,customerList);

        //显示缓存信息
        Map<String, Object> info = new HashMap<String,Object>();
        info.put("customerList",customerList);
        return info;
    }

    /**
     * 步骤1_1：取消
     *
     * @return 实例对象
     */
    @Override
//    取消缓存，让checkNo=null，删除数据库里面的数据
    public boolean cancelAddCustomer(){
        this.checkInformationMapper.deleteByCheckInNo(checkInNo);
        this.removeMapCache(checkInNo);
        checkInNo = null;
        return true;
    }
    /**
     * 删除缓存数据_内存存储_HashMap
     * 步骤2_依赖方法1
     * @param checkInNo
     */
//    删除缓存封装，封装函数在 CheckInformationDetailsServiceImpl
    private void removeMapCache(String checkInNo) {
        CheckInformationDetailsServiceImpl.removeMap(checkInNo, mapCache);
    }

    /**
     * 步骤二 通过预订单办理入住
     *
     */
    @Override
    public boolean checkInByReservation(ReservationVo reservationVo){
        boolean flag, tag;
        roomNo = reservationVo.getRoomNo();
        customerPhone = reservationVo.getCustomerPhone();
        roomTypeId = hotelRoomMapper.queryTypeByRoom(roomNo).getRoomTypeId();
        reservation = reservationMapper.queryByTypeAndPhone(roomTypeId,customerPhone);

        List<CheckInformationDetails> checkInformationDetailsList = new ArrayList<>();
        List<Customer> customerList = mapCache.get(checkInNo);
        //System.out.println(customerList.size());
        int statusId = this.hotelRoomMapper.queryTypeByRoom(roomNo).getStatusId();
        if (!roomStatusMapper.queryById(statusId).getStatusName().equals("已预定")) {
            this.checkInformationMapper.deleteByCheckInNo(checkInNo);
            this.removeMapCache(checkInNo);
            checkInNo = null;
            return false;
        }else {
            for (int i = 0; i < customerList.size(); i++) {
                //插入订单明细表
                CheckInformationDetails checkInformationDetails = new CheckInformationDetails();
                checkInformationDetails.setRoomNo(roomNo);
                checkInformationDetails.setCustomerName(customerList.get(i).getCustomerName());
                checkInformationDetails.setBreakfast(reservation.getBreakfast());
                checkInformationDetails.setStartTime(reservation.getStartTime());
                checkInformationDetails.setEndTime(reservation.getEndTime());
                checkInformationDetails.setCheckInformationId(this.checkInformationMapper.queryIdByCheckInNo(checkInNo).getCheckInformationId());
                checkInformationDetailsList.add(checkInformationDetails);
            }
            flag = this.checkInformationDetailsMapper.insertBatch(checkInformationDetailsList) > 0;

            //插入订单表
            checkInformation.setRoomNo(roomNo);
            checkInformation.setTotalAmount(reservation.getTotalAmount());
            checkInformation.setPeopleNumber(customerList.size());
            checkInformation.setCustomerType(reservation.getCustomerType());
            checkInformation.setStartTime(reservation.getStartTime());
            checkInformation.setPayStatus("已支付");
            tag = this.checkInformationMapper.update(checkInformation) > 0;

            //修改房间状态
            hotelRoom = new HotelRoom();
            hotelRoom.setRoomId(hotelRoomMapper.queryTypeByRoom(roomNo).getRoomId());
            hotelRoom.setStatusId(1);
            hotelRoomMapper.update(hotelRoom);
        }

        //清除缓存
        if (flag && tag) {
            this.removeMapCache(checkInNo);
            checkInNo = null;
            return true;
        }
        return false;
    }

}
